package com.hq.fxh.No5_User.Jianli.Activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hq.fxh.CustomerView.CircleImageView;
import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.GetJianLiEduModle;
import com.hq.fxh.Modle.GetJianLiWorkModle;
import com.hq.fxh.Modle.JianLiEduModle;
import com.hq.fxh.Modle.JianLiModle;
import com.hq.fxh.Modle.JianLiWorkModle;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No0_ChengQu.Login.LoginActivity;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.BooleanUtils;
import com.hq.fxh.No5_User.Jianli.JianLi_EduAdapter;
import com.hq.fxh.No5_User.Jianli.JianLi_WorkAdapter;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.Tool;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static cn.jiguang.analytics.android.d.b;
import static cn.jpush.android.api.JPushInterface.a.s;
import static cn.jpush.android.api.JPushInterface.a.t;
import static com.hq.fxh.R.id.sbSwitch;

public class MyJianLi extends BaseActivity implements View.OnClickListener {

    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected CircleImageView imageJianliUserHeadPic;
    protected TextView tvJianliName;
    protected TextView tvUserJianJie;
    protected TextView btnJianliSetFangXiang;
    protected ListView listWork;
    protected LinearLayout layoutJianliAddWork;
    protected ListView listEducation;
    protected LinearLayout layoutJianliAddEducation;
    protected SwitchButton btnJianliShow;
    protected RelativeLayout layoutSettingSengLiuLiangMode;
    protected RelativeLayout layoutSetUser;
    protected LinearLayout toolbarContent;
    JianLiModle jianliMessage;
    JianLi_EduAdapter educationAdapter;
    JianLi_WorkAdapter workAdapter;
    List<JianLiWorkModle> workData;
    List<JianLiEduModle> eduData;
    public int memberId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_jian_li;
    }

    @Override
    protected void initView() {
        toolbarImageBack = (ImageView) findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(MyJianLi.this);
        toolBarTvTitle = (TextView) findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) findViewById(R.id.toolbar_Layout_More);
        toolbarLayoutMore.setOnClickListener(MyJianLi.this);
        imageJianliUserHeadPic = (CircleImageView) findViewById(R.id.image_jianli_UserHeadPic);
        tvJianliName = (TextView) findViewById(R.id.tv_jianli_Name);
        tvUserJianJie = (TextView) findViewById(R.id.tv_User_JianJie);
        btnJianliSetFangXiang = (TextView) findViewById(R.id.btn_jianli_setFangXiang);
        btnJianliSetFangXiang.setOnClickListener(MyJianLi.this);
        listWork = (ListView) findViewById(R.id.list_Work);
        layoutJianliAddWork = (LinearLayout) findViewById(R.id.layout_jianli_AddWork);
        layoutJianliAddWork.setOnClickListener(MyJianLi.this);
        listEducation = (ListView) findViewById(R.id.list_Education);
        layoutJianliAddEducation = (LinearLayout) findViewById(R.id.layout_jianli_AddEducation);
        layoutJianliAddEducation.setOnClickListener(MyJianLi.this);
        btnJianliShow = (SwitchButton) findViewById(R.id.btn_jianli_Show);
        layoutSettingSengLiuLiangMode = (RelativeLayout) findViewById(R.id.layout_Setting_SengLiuLiangMode);
        layoutSetUser = (RelativeLayout) findViewById(R.id.layout_SetUser);
        layoutSetUser.setOnClickListener(MyJianLi.this);
        toolbarContent = (LinearLayout) findViewById(R.id.toolbar_Content);
    }

    @Override
    protected void initData() {

        toolBarTvMore.setText("保存");
        toolBarTvTitle.setText("我的简历");
        educationAdapter = new JianLi_EduAdapter();
        listEducation.setAdapter(educationAdapter);
        workAdapter = new JianLi_WorkAdapter();

        listEducation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public JianLiEduModle eduItemData;

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                JianLiWorkModle itemAtPosition = null;
                try {
                    eduItemData = (JianLiEduModle) adapterView.getItemAtPosition(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (eduItemData == null) {
                    eduItemData = eduData.get(i);

                }
                String s = new Gson().toJson(eduItemData);
                Intent intent = new Intent(MyJianLi.this, JianLiSet.class);
                intent.putExtra("type", 2);
                intent.putExtra("data", s);
                startActivity(intent);
            }
        });
        listWork.setAdapter(workAdapter);

        listWork.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                JianLiWorkModle itemAtPosition = null;
                try {
                    itemAtPosition = (JianLiWorkModle) adapterView.getItemAtPosition(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (itemAtPosition == null) {
                    itemAtPosition = workData.get(i);

                }
                String s = new Gson().toJson(itemAtPosition);
                Intent intent = new Intent(MyJianLi.this, JianLiSet.class);
                intent.putExtra("type", 1);
                intent.putExtra("data", s);
                startActivity(intent);

            }
        });

        Tool.setListViewHeightBasedOnChildren(listEducation);
        Tool.setListViewHeightBasedOnChildren(listWork);
        getJianLi();

        btnJianliShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setIsPupo(isChecked);

            }
        });


    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.toolbar_Layout_back) {
            finish();
        } else if (view.getId() == R.id.toolbar_Layout_More) {
            finish();
        } else if (view.getId() == R.id.layout_SetUser) {
            Intent intent = new Intent(MyJianLi.this, JianLiSet.class);
            String s = null;
            try {
                s = new Gson().toJson(jianliMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }

            intent.putExtra("type", 0);
            intent.putExtra("data", s);
            startActivity(intent);

        } else if (view.getId() == R.id.btn_jianli_setFangXiang) {
            Intent intent = new Intent(MyJianLi.this, JianLiSet.class);
            intent.putExtra("type", 3);
            startActivity(intent);

        } else if (view.getId() == R.id.layout_jianli_AddWork) {
            Intent intent = new Intent(MyJianLi.this, JianLiSet.class);
            intent.putExtra("type", 1);
            startActivity(intent);
        } else if (view.getId() == R.id.layout_jianli_AddEducation) {
            Intent intent = new Intent(MyJianLi.this, JianLiSet.class);
            intent.putExtra("type", 2);
            startActivity(intent);
        }

    }

    public void getJianLi() {


        memberId = SharedPreferenceUtils.getMemberId(this);
        if (memberId == 0) {
            startActivity(new Intent(MyJianLi.this, LoginActivity.class));
            return;
        }
        getJianLiMessage(memberId + "");
        getJianLiWork(memberId + "");
        getJianLiEdu(memberId + "");

    }

    private void getJianLiWork(String memberId) {
        UserServiceImpl.getJianLiWork(memberId, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    ResponseBody body = response.body();
                    if (body == null) {
                        ResponseBody responseBody = response.errorBody();
                        if (responseBody != null) {
                            String string1 = responseBody.string();
                            LogUtils.e("工作：错误信息" + string1);
                        }
                    } else {
                        String string = body.string();
                        LogUtils.e("工作：" + string);
                        GetJianLiWorkModle getJianLiWorkModle = new Gson().fromJson(string, GetJianLiWorkModle.class);
                        workData = getJianLiWorkModle.getData();
                        workAdapter.setData(workData);
                        workAdapter.notifyDataSetChanged();
                        Tool.setListViewHeightBasedOnChildren(listWork);

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("工作：错误信息" + t.getMessage());

            }
        });


    }

    private void getJianLiEdu(String memberId) {
        UserServiceImpl.getJianLiEdu(memberId, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    ResponseBody body = response.body();
                    if (body == null) {
                        ResponseBody responseBody = response.errorBody();
                        if (responseBody != null) {
                            String string1 = responseBody.string();
                            LogUtils.e("教育：错误信息1" + string1);
                        }
                    } else {
                        String string = body.string();
                        LogUtils.e("教育：" + string);
                        GetJianLiEduModle getJianLiWorkModle = new Gson().fromJson(string, GetJianLiEduModle.class);
                        eduData = getJianLiWorkModle.getData();
                        educationAdapter.setData(eduData);
                        educationAdapter.notifyDataSetChanged();
                        Tool.setListViewHeightBasedOnChildren(listEducation);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                LogUtils.e("教育：错误信息" + t.getMessage());

            }
        });


    }


    private void getJianLiMessage(String memberId) {


        UserServiceImpl.getJianLi("" + memberId, new Callback<JianLiModle>() {
            @Override
            public void onResponse(Call<JianLiModle> call, Response<JianLiModle> response) {
                JianLiModle body = response.body();
                if (body == null) {
                    ResponseBody responseBody = response.errorBody();
                    if (responseBody != null) {
                        try {
                            LogUtils.e(responseBody.string());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    JianLiModle body1 = response.body();
                    if (body1 == null) {
                        String msg = body.getMsg();
                        LogUtils.e("msg==" + msg);
                    } else {
                        LogUtils.e(body1.toString());
                        jianliMessage = body1;
                        String header_url = (String) body1.getData().getHeader_url();
                        Tool.setUrlToHeadImage(MyJianLi.this, header_url, imageJianliUserHeadPic);
                        Tool.setStringToText(tvJianliName, body1.getData().getName());
                        int is_public = body1.getData().getIs_public();
                        if (is_public == 0) {
                            btnJianliShow.setChecked(true);
                        } else {
                            btnJianliShow.setChecked(false);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<JianLiModle> call, Throwable t) {
                if (t != null) {
                    LogUtils.e(t.getMessage());

                } else {
                    LogUtils.e("error");

                }

            }
        });
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        getJianLi();
    }


    public void setIsPupo(boolean isPupo) {
        //0不公开  1 公开
        int status = 0;
        if (isPupo) {
            status = 0;
        } else {
            status = 1;
        }
        UserServiceImpl.SetResumePublic(memberId, status, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody body = response.body();
                if (body == null) {
                    ResponseBody responseBody = response.errorBody();
                    if (responseBody != null) {
                        try {
                            LogUtils.e(responseBody.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {

                    try {
                        LogUtils.e(body.string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                if (t != null) {
                    LogUtils.e(t.getMessage());

                } else {
                    LogUtils.e("error");

                }

            }
        });
    }
}
